package com.top.zk.pandachannel.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.internal.Streams;
import com.top.zk.pandachannel.IMapBiz;
import com.top.zk.pandachannel.R;
import com.top.zk.pandachannel.adapter.CultureRecyclerviewAdapter;
import com.top.zk.pandachannel.adapter.EyeRecyclerviewAdapter;
import com.top.zk.pandachannel.bean.CultureBean;
import com.top.zk.pandachannel.help.GlideImageLoader;
import com.top.zk.pandachannel.help.SpacesItemDecoration;
import com.top.zk.pandachannel.util.URLConstants;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class PandaCultureFragment extends Fragment {

    private Context mContext = null;
    private RecyclerView recyclerView_culture;
    private Banner banner;
    private CultureRecyclerviewAdapter mAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_panda_culture, container, false);
    }

    private void initData() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(URLConstants.BASEURL)
                .addConverterFactory(ScalarsConverterFactory.create()).build();
        IMapBiz iMapBiz = retrofit.create(IMapBiz.class);
        Call<String> call = iMapBiz.getCultureBeanjson();

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccess()){
                    String body = response.body();
                    Gson gson = new Gson();
                    CultureBean cultureBean = gson.fromJson(body, CultureBean.class);
                    List<CultureBean.BigImgBean> list = cultureBean.getBigImg();

                    List<String> imgList = new ArrayList<String>();
                    List<String> titleList = new ArrayList<String>();
                    for (int i = 0; i < list.size() ; i++) {
                        imgList.add(list.get(i).getImage());
                        titleList.add(list.get(i).getTitle());
                    }
                    //轮播图
                    banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
                    banner.setImageLoader(new GlideImageLoader());
                    banner.setImages(imgList);
                    banner.setBannerTitles(titleList);
                    banner.isAutoPlay(true);
                    banner.setDelayTime(4000);
                    banner.setIndicatorGravity(BannerConfig.CENTER);
                    banner.start();

                    //item
                    List<CultureBean.ListBean> list2 = cultureBean.getList();
                    //创建适配器
                    mAdapter = new CultureRecyclerviewAdapter(mContext,list2);
                    //设置适配器
                    recyclerView_culture.setAdapter(mAdapter);
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        banner = (Banner) view.findViewById(R.id.banner_culture);
        recyclerView_culture = (RecyclerView) view.findViewById(R.id.recyclerView_culture);
        LinearLayoutManager layoutManager =
                new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false);
        recyclerView_culture.setLayoutManager(layoutManager);
        recyclerView_culture.addItemDecoration(new SpacesItemDecoration(10));

        initData();
    }
}
